package com.Util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 数据库的工具类
 */

public class DruidUtil {

    //声明数据源对象
    private static Connection connection = null;
    private static DataSource dataSource = null;

    //创建数据库连接池
    public static void init()
    {

        //1.加载配置文件
//        Properties properties = new Properties();
//        properties.setProperty("driverClassName", "com.mysql.jdbc.Driver");
//        properties.setProperty("url", "jdbc:mysql://localhost:3306/dormitory?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=utf-8");
//        properties.setProperty("username", "root");
//        properties.setProperty("password", "root");
//        properties.setProperty("initialSize", "5");
//        properties.setProperty("maxActive", "20");
//        properties.setProperty("maxWait", "3000");
//        properties.setProperty("timeBetweenEvictionRunsMillis", "60000");
//        properties.setProperty("minEvictableIdleTimeMillis", "300000");
//        properties.setProperty("validationQuery", "SELECT 1");
//        properties.setProperty("testWhileIdle", "true");
//        properties.setProperty("testOnBorrow", "false");
//        properties.setProperty("testOnReturn", "false");
//        properties.setProperty("poolPreparedStatements", "true");
//        properties.setProperty("maxPoolPreparedStatementPerConnectionSize", "20");
//        properties.setProperty("filters", "stat");
//        properties.setProperty("connectionProperties", "druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
//
//        //2.获取文件的路径
//        InputStream inputStream = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties");
//
//        //3.把文件流传入到连接池中去
//        try {
//            properties.load(inputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        //4.初始化Druid连接池
//        try {
//
//            dataSource = DruidDataSourceFactory.createDataSource(properties);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//        //5.关闭输入流
//        try {
//            inputStream.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    //返回Connection对象
    public static Connection getConnection() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/dormitory?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=utf-8";
        String user = "root";
        String pwd = "root";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    public static void close()
    {
        if (connection != null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //归还连接,有结果集的
    public static void close(ResultSet resultSet, Connection connection, Statement statement)
    {
        //1.关闭结果集
        if (resultSet != null)
        {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        //2.关闭执行SQL的句柄
        if (statement != null)
        {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        //3.关闭连接
        if (connection != null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //归还连接,没有结果集的
    public static void close(Statement statement, Connection connection)
    {
        if (statement != null)
        {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (connection != null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //获取数据源对象
    public static DataSource getDataSource()
    {
        return dataSource;
    }
}
